package com.chenchao.reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @encoding : UTF-8
 * @Time : 2020/12/2 14:14
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javareflection.Test06.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class Test06 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> aClass = Class.forName("com.chenchao.reflection.Fish");
        Annotation[] annotations = aClass.getAnnotations();
        /*获取所有注解信息*/
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
            //@com.chenchao.javareflection.FishInfo(name=cat, age=18)

            /*获取注解指定属性的值*/
            FishInfo annotation1 = (FishInfo) annotation;
            String name = annotation1.name();
            System.out.println("类注解的name属性为"+name);
            //类注解的name属性为cat
        }
        /*获取字段注解*/
        Field name = aClass.getField("name");
        FishFieldInfo annotation = name.getAnnotation(FishFieldInfo.class);
        String name1 = annotation.name();
        System.out.println("字段注解的name属性为"+name1);
        //字段注解的name属性为big
    }
}
